package cn.iocoder.yudao.module.security.controller.admin.devicelog;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.devicelog.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.devicelog.SecurityDeviceLogDO;
import cn.iocoder.yudao.module.security.service.devicelog.SecurityDeviceLogService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 终端日志")
@RestController
@RequestMapping("/security/device-log")
@Validated
public class SecurityDeviceLogController {

    @Resource
    private SecurityDeviceLogService deviceLogService;

    @PostMapping("/create")
    @Operation(summary = "创建终端日志")
    @PreAuthorize("@ss.hasPermission('security:device-log:create')")
    public CommonResult<Long> createDeviceLog(@Valid @RequestBody SecurityDeviceLogSaveReqVO createReqVO) {
        return success(deviceLogService.createDeviceLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新终端日志")
    @PreAuthorize("@ss.hasPermission('security:device-log:update')")
    public CommonResult<Boolean> updateDeviceLog(@Valid @RequestBody SecurityDeviceLogSaveReqVO updateReqVO) {
        deviceLogService.updateDeviceLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除终端日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:device-log:delete')")
    public CommonResult<Boolean> deleteDeviceLog(@RequestParam("id") Long id) {
        deviceLogService.deleteDeviceLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得终端日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:device-log:query')")
    public CommonResult<SecurityDeviceLogRespVO> getDeviceLog(@RequestParam("id") Long id) {
        SecurityDeviceLogDO deviceLog = deviceLogService.getDeviceLog(id);
        return success(BeanUtils.toBean(deviceLog, SecurityDeviceLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得终端日志分页")
    @PreAuthorize("@ss.hasPermission('security:device-log:query')")
    public CommonResult<PageResult<SecurityDeviceLogRespVO>> getDeviceLogPage(@Valid SecurityDeviceLogPageReqVO pageReqVO) {
        PageResult<SecurityDeviceLogDO> pageResult = deviceLogService.getDeviceLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecurityDeviceLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出终端日志 Excel")
    @PreAuthorize("@ss.hasPermission('security:device-log:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceLogExcel(@Valid SecurityDeviceLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecurityDeviceLogDO> list = deviceLogService.getDeviceLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "终端日志.xls", "数据", SecurityDeviceLogRespVO.class,
                        BeanUtils.toBean(list, SecurityDeviceLogRespVO.class));
    }

}